var timer;

$(document).ready(function() {
    $("#search").val("Enter your search here...");
    $('#search').bind({
        click: function() {
            $(this).val("");
        },
        keypress: function(event) {
            if (event.keyCode == '13') {
                searchCJAX();
            } 
            else {
                searchChanged();
            }
        }
        
    });
});

function searchChanged()
    {
        if(timer) {
            clearTimeout(timer);
        }   
        timer = setTimeout("searchCJAX()", 600);

    }

function searchCJAX() {
    $("#searchingFor").text("Searching for " + $("#search").val());
    $("#search").addClass("searchLoading");
    $.getJSON(REST_URL + "cjax/" + edition + "/" + catalog + "/keyword/" + $("#search").val(), function(data) {
        $("#searchingFor").empty();
        $(".searchResultsWrapper").empty();
        $("#search").removeClass("searchLoading");
        var length;
        if (data.length > 5) {
            length = 5;
        }
        else {
            length = data.length;
        }
        for (var i = 0; i < length; i++) {
            $(".searchResultsWrapper").append("<div class=\"result\" id=result-"+ data[i].bibrecord + "><div class=\"resultTitle\"> </div><div class=\"resultInfo\"> </div></div>"); 
            $("#result-" + data[i].bibrecord + " > .resultTitle").text(data[i].title);
        }    
        
    });
}
